﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Access.PowerTools.AbstractionLayer
{
    public partial class AccessDatabase
    {
        private AccessDatabase() {}

        private IHostComunicationBridge _iHostBridge;
        public AccessDatabase(IHostComunicationBridge iHostBridge)
        {
            _iHostBridge = iHostBridge;
        }


        public List<AccessDatabaseObjectInfo> AllObjects
        {
            get
            {
                List<AccessDatabaseObjectInfo> allObjects = _iHostBridge.GetObjectsList();
                allObjects.Sort(comparison);   
                return allObjects;
            }
        }

        private int comparison(AccessDatabaseObjectInfo o1, AccessDatabaseObjectInfo o2)
        {
            if (o1.Type > o2.Type) return 1;
            if (o1.Type < o2.Type) return -1;
            return string.Compare(o1.ObjectTypeAndName, o2.ObjectTypeAndName);
        }

        public string FullPath
        {
            get
            {
                return _iHostBridge.CurrentDatabaseFullPath;  
            }
        }

        public AccessDatabaseObjectInfo GetObjectInfo(AccessDataBaseObjectTypeEnumeration type,
               string name)
        {
            AccessDatabaseObjectInfo objectInfo = null;
            bool test = false;
            switch (type)
            {
                case AccessDataBaseObjectTypeEnumeration.Table:
                    test = AllTables.TryGetValue(name, out objectInfo); break;
                case AccessDataBaseObjectTypeEnumeration.Query:
                    test = AllQueries.TryGetValue(name, out objectInfo); break;
                case AccessDataBaseObjectTypeEnumeration.Form:
                    test = AllForms.TryGetValue(name, out objectInfo); break;
                case AccessDataBaseObjectTypeEnumeration.Report:
                    test = AllReports.TryGetValue(name, out objectInfo); break;
                case AccessDataBaseObjectTypeEnumeration.Macro:
                    test = AllMacros.TryGetValue(name, out objectInfo); break;
                case AccessDataBaseObjectTypeEnumeration.Module:
                    test = AllModules.TryGetValue(name, out objectInfo); break;
                default:
                    throw new ApplicationException(
                        string.Format("{0}.{1}: Undefined object type {2}",
                        System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.Name,
                        System.Reflection.MethodInfo.GetCurrentMethod().Name,
                        type));
  
            }
            return objectInfo;
        }

        public Dictionary<string, AccessDatabaseObjectInfo> AllTables
        {
            get { return getAllObjects(AccessDataBaseObjectTypeEnumeration.Table); }
        }
        public Dictionary<string, AccessDatabaseObjectInfo> AllQueries
        {
            get { return getAllObjects(AccessDataBaseObjectTypeEnumeration.Query ); }
        }
        public Dictionary<string, AccessDatabaseObjectInfo> AllForms
        {
            get { return getAllObjects(AccessDataBaseObjectTypeEnumeration.Form ); }
        }
        public Dictionary<string, AccessDatabaseObjectInfo> AllReports
        {
            get { return getAllObjects(AccessDataBaseObjectTypeEnumeration.Report); }
        }
        public Dictionary<string, AccessDatabaseObjectInfo> AllMacros
        {
            get { return getAllObjects(AccessDataBaseObjectTypeEnumeration.Macro); }
        }
        public Dictionary<string, AccessDatabaseObjectInfo> AllModules
        {
            get { return getAllObjects(AccessDataBaseObjectTypeEnumeration.Module); }
        }

        private Dictionary<string, AccessDatabaseObjectInfo> getAllObjects(AccessDataBaseObjectTypeEnumeration type)
        {
            Dictionary<string, AccessDatabaseObjectInfo> list = new Dictionary<string, AccessDatabaseObjectInfo>();
            foreach (AccessDatabaseObjectInfo o in AllObjects)
                if (o.Type == type) list.Add(o.Name, o); 
            return list;
        }
    }
}
